<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>城市信息管理界面</title>
    <link href="<%=request.getContextPath()%>/fare/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
    <script src="<%=request.getContextPath()%>/fare/lib/jquery/jquery-1.5.2.min.js" type="text/javascript"></script> 
    <script src="<%=request.getContextPath()%>/fare/lib/ligerUI/js/core/base.js" type="text/javascript"></script>
    <script src="<%=request.getContextPath()%>/fare/js/ligerUI/js/ligerui.min.js" type="text/javascript"></script> 
    
     <script src="<%=request.getContextPath()%>/fare/js/json2.js" type="text/javascript"></script> 
    
    <script type="text/javascript">
        var gridManager = null;
        var columns = [
                { display: '城市名称', name: 'name'},
                { display: '城市编码', name: 'code'},
                { display: '所属区域', name: 'area'},
                { display: '区域码', name: 'areaCode'},
                { display: '机场名称', name: 'airportName'},
                { display: '所属省份', name: 'province' },
                { display: '城市拼音', name: 'nameSpell' },
                { display: '拼音简写', name: 'nameSpellInitials' },
                { display: '所属国家', name: 'countryName'},
                { display: '国家二字码', name: 'countryNameCode',align:'center',width:80},
                { display: '是否热门城市', name: 'hotlever',align:'center',width:80,
                	render: function (record, rowindex, value, column){
                		var hotlevers=record.hotlever;
                	 if(hotlevers>0){
                		 return "是";
                	 }if(hotlevers==0){
                		 return "否";
                		 }
                	}
                },
                { display: '是否国际城市', name: 'isInternational',align:'center',width:80,
                	render: function (record, rowindex, value, column){
                		var isInternational=record.isInternational;
                	 if(isInternational==1){
                		 return "是";
                	 }if(isInternational==0){
                		 return "否";
                		 }
                	}
                },
                { display: '所属洲际', name: 'interArea'},
                { display: '洲际代码', name: 'interAreaCode'},
                { display: '城市人口', name: 'population'},
                { display: '人均GDP', name: 'gdp'}
                ];
        $(function ()
        {
        	 //工具条
            $("#toptoolbar").ligerToolBar({ items: [
                { text: '增加', id:'add', click: itemclick },
                { text: '修改', id:'modify', click: itemclick },
                { text: '删除', id:'delete', click: itemclick }
            	]
         	});
            //初始化表格
           init();
        });

        function init(){
        	$.getJSON("<%=request.getContextPath()%>/cityManageAction!showCitys.action?1="+Math.random(),function(data){
 				$("#maingrid").ligerGrid({
	                columns: columns, 
					dataAction: 'server', 
					data: data,
	                sortName: 'name',
	                width: '100%', height: '100%', pageSize: 30,rownumbers:true,
	                checkbox : true,
	                heightDiff: -6
	            });
	            gridManager = $("#maingrid").ligerGetGridManager();
	            $("#pageloading").hide();
           });
        }
        
        function itemclick(item)
        { 
            if(item.id)
            {
                switch (item.id)
                {
                    case "add":
                		add();
                		return;
                    case "modify":
                    	modify();
                    	return;
                    case "delete":
                        delete2();
                        return;
                }   
            }
        }
        
		function add(){
        	$.ligerDialog.open({
				width : 300,
				height :300,
				target : $("#cityEditForm"),
				title : '新增城市信息',
				buttons : [ {
					text : '确定',
					onclick : function(item, dialog) {
						if(checkForm()){
							dialog.hidden();
							$.ajax({
								cache : false,
								type : "post",
								data: $("#cityEditForm").serialize(), 
								url : "<%=request.getContextPath()%>/cityManageAction!saveOrUpdate.action",
								success : function(data){
									$.ligerDialog.alert('添加成功！', '提示', "success",function(){
										location.href = "<%=request.getContextPath()%>/fare/cityManage/cityManage.jsp";
									});
								},
								error : function(){
									$.ligerDialog.error('服务器出错，操作失败，请重试!');
								}
							});
						}
					}
				}, {
					text : '取消',
					onclick : function(item, dialog) {
						//$('#editForm span').text('');
						dialog.hidden();
					}
				} ]
			});
        }
        
        function modify(){
        	var cityID = null;
        	var rowsdata = gridManager.getCheckedRows();
        	if (!rowsdata.length) {
        	 	alert('请选择一行进行修改');
        	 	return;
        	}
        	if(rowsdata.length > 1){
        		alert('修改只能选一行');
        	 	return;
        	}
        	$("#cityID").attr("value",rowsdata[0].id);
        	cityID = rowsdata[0].id;
        	$("#cityName").attr("value",rowsdata[0].name);
        	$("#cityCode").attr("value",rowsdata[0].code);
        	$("#airportName").attr("value",rowsdata[0].airportName);
        	$("#province").attr("value",rowsdata[0].province);
        	$("#area").attr("value",rowsdata[0].area);
        	$("#population").attr("value",rowsdata[0].population);
        	$("#GDP").attr("value",rowsdata[0].gdp);
        	$.ligerDialog.open({
				width : 300,
				height :300,
				target : $("#cityEditForm"),
				title : '编辑城市信息',
				buttons : [ {
					text : '确定',
					onclick : function(item, dialog) {
						if(checkForm()){
							//这句代码不是很懂
							dialog.hidden();
							$.ajax({
								cache : false,
								type : "post",
								data: $("#cityEditForm").serialize(), 
								url : "<%=request.getContextPath()%>/cityManageAction!saveOrUpdate.action?cityId="+cityID,
								success : function(data){
									$.ligerDialog.alert('修改成功', '提示', "success",function(){
										location.href = "<%=request.getContextPath()%>/fare/cityManage/cityManage.jsp";
									});
								},
								error : function(){
									$.ligerDialog.error('服务器出错，操作失败，请重试!');
									}
							});
						}
					}
				}, {
					text : '取消',
					onclick : function(item, dialog) {
						dialog.hidden();
					}
				} ]
			});
        }
        function delete2(){
        	var data = gridManager.getCheckedRows();
                        if (data.length == 0)
                            alert('请选择行');
                        else
                        {
                            var checkedIds = [];
                            $(data).each(function ()
                            {
                                checkedIds.push(this.id);
                            });
                            $.ligerDialog.confirm('确定删除?', function ()
                            {
					    		$.post("<%=request.getContextPath()%>/cityManageAction!delete.action", [ {
					    				name : 'ids',
					    				value : checkedIds
					    			} ], function() {
					    				alert("删除成功");
				    			});
				    			gridManager.deleteSelectedRow();
                            }); 
                        }
        }
        
        function find(){
        	var name= $("#name_select").val();
        	//var name=encodeURI($("#name_select").val())；
        	var code= $("#code_select").val();
			$.getJSON("<%=request.getContextPath()%>/cityManageAction!findCityByConditions.action?name="+encodeURI(encodeURI(name))+"&code="+code,function(data){
				$("#maingrid").ligerGrid({
	                columns: columns, 
					dataAction: 'server', 
					data: {Rows:data},
	                sortName: 'name',
	                width: '100%', height: '100%', pageSize: 30,rownumbers:true,
	                checkbox : true,
	                heightDiff: -6
	            });
	            gridManager = $("#maingrid").ligerGetGridManager();
	
	            $("#pageloading").hide();
           });
        }
        
        //验证非空 
	function checkForm() {
		
		//判断输入的值是否为数字
		var strP=/^\d+$/;
		var flag=true;
		var dataFlag = true;
		if ($("#cityName").val() == ''||$("#cityCode").val() == ''||$("#area").val()==''
		||$("#airportName").val() == ''||$("#province").val()==''||$("#population").val() == ''
		||$("#GDP").val() == '') {
			flag = false;
			alert("有内容未填");
		}
		
		if (!strP.test($("#population").val())) {
			dataFlag = false;
			alert("城市人口应该输入数字！");
		}
		
		if (!strP.test($("#GDP").val())) {
			dataFlag = false;
			alert("人均GDP应该输入数字！");
		}
		return flag&&dataFlag;
	}
	
    </script>
</head>
<body style="padding:0px; overflow:hidden;"> 
	<div class="l-loading" style="display:none" id="pageloading"></div> 
	<div id="toptoolbar"></div> 
	<div>
		 <form id="findForm"> 
	<div class="l-panel-search">
    <div class="l-panel-search-item">
         城市名称：<input type="text" style="width: 100px" id="name_select"	name="name" value="">
    </div>
    <div class="l-panel-search-item">
        城市编码：<input type="text" style="width: 100px" id="code_select"	name="code" value="">
    </div>
    <div class="l-panel-search-item">
    	<input type="button" value="查询" onclick="javascript:find();" style="width:80px;">
    </div>
</div>
  </form>
	</div>
	<div id="maingrid" style="margin:0; padding:0"></div>
 
  
 <div  style="display:none">
		<form  method="post" id="cityEditForm">
			<input type="hidden" name="city.id" >
			城市名称:<input type="text" name="city.name" id="cityName"><br>
			城市编码:<input type="text" name="city.code" id="cityCode"><br>
			所属区域:<input type="text" name="city.area" id="area"><br>
			机场名称:<input type="text" name="city.airportName" id="airportName"><br>
			所属省份:<input type="text" name="city.province" id="province"><br>
			城市人口:<input type="text" name="city.population" id="population"><br>
		        人均GDP:<input type="text" name="city.GDP" id="GDP"><br>
		</form>
	</div>
</body>
</html>
